package dev.ece.suit.bloom;

import java.io.InputStream;

import org.lwjgl.opengl.GL20;

import dev.ece.core.shader.Shader;
import dev.ece.core.texture.Texture;

public class BloomHDRShader extends Shader {

	private int textureHandler;
	
	private int brightHandler;
	
	private int enableHandler;
	
	private int gammaHandler;
	
	private int exposureHandler;
	
	public BloomHDRShader(InputStream vertexShaderInputStream,
			InputStream pixelShaderInputStream) {
		super(vertexShaderInputStream, pixelShaderInputStream);
	}
	
	@Override
	protected void onInitializeHandler(int program) {
		textureHandler = glGetUniformLocation("texture");
		brightHandler = glGetUniformLocation("bright");
		enableHandler = glGetUniformLocation("enable");
		gammaHandler = glGetUniformLocation("gamma");
		exposureHandler = glGetUniformLocation("exposure");
	}

	@Override
	protected void onBindAttribLocations(int program) {
		bindAttribLocation("vao_Vertex");
		bindAttribLocation("vao_TexCoord");
	}
	
	public void glEnable(boolean enable) {
		GL20.glUniform1i(enableHandler, enable ? 1 : 0);
	}
	
	public void glGamma(float gamma) {
		GL20.glUniform1f(gammaHandler, gamma);
	}
	
	public void glExposure(float exposure) {
		GL20.glUniform1f(exposureHandler, exposure);
	}
	
	public void glTexture(Texture texture) {
		GL20.glUniform1i(textureHandler, texture.bind(0));
	}
	
	public void glBright(Texture texture) {
		GL20.glUniform1i(brightHandler, texture.bind(1));
	}

	public static BloomHDRShader createBloomHDRShader() {
		BloomHDRShader shader = new BloomHDRShader(
				BloomHDRShader.class.getClassLoader().getResourceAsStream("shaders/bloomhdr.vert"),
				BloomHDRShader.class.getClassLoader().getResourceAsStream("shaders/bloomhdr.frag"));
		return shader;
	}

	@Override
	public void clearup() {
		GL20.glDeleteShader(this.brightHandler);
		GL20.glDeleteShader(this.enableHandler);
		GL20.glDeleteShader(this.textureHandler);
		GL20.glDeleteShader(this.gammaHandler);
		GL20.glDeleteShader(this.exposureHandler);
		super.clearup();
	}
	
}
